#!/usr/bin/perl -c

package PIPL::Configuration::Path;

=head1 NAME

PIPL::Configuration::Path - Find paths for PI configuration directory

=head1 SYNOPSIS

  use PIPL::Configuration::Path;
  $p = PIPL::Configuration::Path->instance;
  @confdirs = @{ $p->confdirs }; # configuration lookup path

=head1 DESCRIPTION

This singleton class provides an information about PI's paths for
configuration directory.

=cut


use strict;


use base 'PIPL::Class::Base',
         'PIPL::Class::Singleton';


use File::Spec;
use PIPL::System::Env;
use PIPL::System::Path;
use PIPL::System::Filesystem;


# List of class fields (name => {is=>ro|rw, default=>value})
use constant FIELDS => {
    confdirs => { is => 'ro' },
};


# Returns a list of possible directories for configuration directory
sub confdirs {
    my $self = shift; $self = __PACKAGE__->instance unless ref $self eq __PACKAGE__;
    return $self->{confdirs} if defined $self->{confdirs};

    my @confdirs;

    my $is_unix = $PIPL::System::Filesystem::Self->{is_Unix} || $PIPL::System::Filesystem::Self->{is_Cygwin};
    my $is_windows = $PIPL::System::Filesystem::Self->{is_Win32};

    my $PERL_PIPL_DIR = $PIPL::System::Env::Self->{PERL_PIPL_DIR};

    push @confdirs, $PERL_PIPL_DIR
        if $PERL_PIPL_DIR;

    push @confdirs, File::Spec->catdir(PIPL::System::Path->home,
        ($is_unix ? '.pi.pl' : 'PI.PL'));

    push @confdirs, File::Spec->catdir(PIPL::System::Path->appdata, 'PI.PL')
        if $is_windows;

    push @confdirs, '/etc/pi.pl'
        if $is_unix;

    return $self->{confdirs} = [ @confdirs ];
}


1;


=head1 BASE CLASSES

=over 2

=item *

L<PIPL::Class::Base>

=item *

L<PIPL::Class::Singleton>

=back

=head1 FIELDS

=over

=item confdirs (ro)

Contains the reference to the cached list of configuration directories.

=back

=head1 CONSTRUCTOR

=head2 instance

Return existing instance of class or create new object.

=head1 METHODS

=head2 confdirs

Returns the reference to list of possible directories for configuration
directory.

  foreach (@{ PIPL::Configuration::Path->confdirs }) {
    if (-w $_) {
      print "found writable configuration directory";
      last;
    }
  }

The configuration directory list is based on following schema:

=over 2

=item *

F<$PERL_PIPL_DIR>

=item *

F<$HOME/.pi.pl> (only for Unix and Cygwin)

=item *

F<$HOME/PI.PL> (only for Windows)

=item *

F<$APPDATA/PI.PL> (only for Windows)

=item *

F</etc/pi.pl> (only for Unix and Cygwin)

=back

=head1 AUTHORS

Piotr Roszatycki E<lt>dexter@debian.orgE<gt>

=head1 COPYRIGHT

Copyright 2006-2007 by Piotr Roszatycki E<lt>dexter@debian.orgE<gt>.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

See L<http://www.perl.com/perl/misc/Artistic.html>
